package com.fc.mall.service.impl;

import com.fc.mall.dto.DeliveryTempParams;
import com.fc.mall.exception.ServiceException;
import com.fc.mall.mapper.PmsDeliveryTemplateMapper;
import com.fc.mall.mapper.PmsFeightTemplateMapper;
import com.fc.mall.model.PmsDeliveryTemplate;
import com.fc.mall.model.PmsDeliveryTemplateExample;
import com.fc.mall.model.PmsFeightTemplate;
import com.fc.mall.model.PmsFeightTemplateExample;
import com.fc.mall.service.PmsDeliveryTemplateService;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Sunny on 2019/6/23.
 */
@Service
public class PmsDeliveryTemplateServiceImpl implements PmsDeliveryTemplateService {

    @Autowired
    private PmsDeliveryTemplateMapper deliveryTemplateMapper;

    @Autowired
    private PmsFeightTemplateMapper feightTemplateMapper;

    @Autowired
    MapperFacade mapperFacade;

    @Override
    public List<PmsDeliveryTemplate> list() {
        PmsDeliveryTemplateExample example = new PmsDeliveryTemplateExample();
        example.createCriteria();
        return deliveryTemplateMapper.selectByExample(example);
    }

    @Override
    public DeliveryTempParams get(Long id) {
        PmsDeliveryTemplate deliveryTemplate = deliveryTemplateMapper.selectByPrimaryKey(id);
        if (null == deliveryTemplate)
        {
            throw new ServiceException("当前运费模板不存在");
        }
        DeliveryTempParams deliveryTempParams = new DeliveryTempParams();
        mapperFacade.map(deliveryTemplate, deliveryTempParams);
        PmsFeightTemplateExample example = new PmsFeightTemplateExample();
        example.createCriteria().andDeliveryTempIdEqualTo(id);
        List<PmsFeightTemplate> freightTemps =  feightTemplateMapper.selectByExample(example);
        deliveryTempParams.setDeliverFreights(freightTemps);
        return deliveryTempParams;
    }

    @Override
    public int addOrUpdateDelivery(DeliveryTempParams deliveryTempParams) {
        //重名则抛异常
        PmsDeliveryTemplateExample deliveryTemplateExample = new PmsDeliveryTemplateExample();
        PmsDeliveryTemplateExample.Criteria criteria = deliveryTemplateExample.createCriteria();
        criteria.andNameEqualTo(deliveryTempParams.getName());
        List<PmsDeliveryTemplate> list = deliveryTemplateMapper.selectByExample(deliveryTemplateExample);
        if (!list.isEmpty()){
            throw new ServiceException("该模板名称已存在！");
        }

        if(null == deliveryTempParams.getId())
        {
            PmsDeliveryTemplate deliveryTemplate = deliveryTempParams;
            deliveryTemplateMapper.insertSelective(deliveryTemplate);
            Long deliveryId = deliveryTemplate.getId();
            List<PmsFeightTemplate> freightList = deliveryTempParams.getDeliverFreights();
            if(null != freightList && !freightList.isEmpty())
            {
                freightList.forEach(freight -> {
                    freight.setDeliveryTempId(deliveryId);
                    feightTemplateMapper.insertSelective(freight);
                });
            }
        }
        else
        {
            PmsDeliveryTemplate deliveryTemplate = deliveryTempParams;
            deliveryTemplateMapper.updateByPrimaryKey(deliveryTemplate);
            Long deliveryId = deliveryTemplate.getId();
            List<PmsFeightTemplate> freightList = deliveryTempParams.getDeliverFreights();
            if(null != freightList && !freightList.isEmpty())
            {
                PmsFeightTemplateExample example = new PmsFeightTemplateExample();
                example.createCriteria().andDeliveryTempIdEqualTo(deliveryId);
                feightTemplateMapper.deleteByExample(example);
                freightList.forEach(freight -> {
                    freight.setDeliveryTempId(deliveryId);
                    feightTemplateMapper.insertSelective(freight);
                });
            }
        }
        return 0;
    }

    @Override
    public int del(Long id) {
        PmsFeightTemplateExample example = new PmsFeightTemplateExample();
        example.createCriteria().andDeliveryTempIdEqualTo(id);
        feightTemplateMapper.deleteByExample(example);
        return deliveryTemplateMapper.deleteByPrimaryKey(id);
    }
}
